from os.path import exists, join
from Document import Document
from Lexicon import Lexicon
from config import *
from Query import Query

class  QueryManager:

	def create(self):
		query_id = 0
		query_file = open(query_path, 'w')
		for i in xrange(doccount):
			paper = Document(i)
			comment_list = paper.getComments()
			for j in xrange(len(comment_list)):
				query_file.write('queryid='+str(query_id)+'\n')
				query_file.write('ans_doc='+str(i)+'\n')
				query_file.write('query='+comment_list[j].strip()+'\n')
				query_id = query_id + 1
		query_file.close()
	
	def load(self):
		self.query_list = []
		query_file = open(query_path, 'r')
		lines = query_file.readlines()
		query_file.close()

		querycount = (len(lines) + 1) / 3
		for i in xrange(0, len(lines), 3):
			query = Query(lines[i].split('=')[1].strip(),
					int(lines[i+1].split('=')[1].strip()),lines[i+2].split('=')[1].strip())
			self.query_list.append(query)
'''
根据query_id来获取query对象
'''	
	def getQuery(self, query_id):
		return self.query_list[query_id]

	def getSize(self):
		return len(self.query_list)

if __name__ == '__main__':
	qm = QueryManager()
	qm.create()
	qm.load()
	print qm.getQuery(28911)
	print qm.getSize()
